//
//  YKMineViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/9/24.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKMineViewController.h"
#import "YKPerformanceListViewController.h"
#import "YKClientNeedsViewController.h"
#import "YKMineResultViewController.h"
#import "YKBasicInfoViewController.h"
#import "YKApproveListViewController.h"
#import "YKClientVisitManageController.h"
#import "YKClientOrderManageController.h"
#import "YKClientDataManageController.h"
#import "YKTabBarViewController.h"
#import "YKHelpDocumentsViewController.h"
#import "YKPopupView.h"
#import "YKModifyPasswordViewController.h"
#import <TFTextFieldWithTitleItem.h>
@interface YKMineViewController ()
@property (nonatomic, strong) TFTextFieldWithTitleItem *photoItem;
@property (nonatomic, strong) TFTextFieldWithTitleItem *emailItem;
@property(nonatomic, strong) TFTextFieldWithTitleItem *accountItem;
@end

@implementation YKMineViewController
//- (void)loadView {
//    self.view = [[UIScrollView alloc]init];
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationItem.title = @"个人中心";
    self.title = @"我的";
    YKTabBarViewController *tab = (YKTabBarViewController *)self.tabBarController;
    
    UIImageView* topImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"user_index_header_bg"]];
    [self.view addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.width.equalTo(self.view.mas_width);
    }];
    
    UIImageView* avatarIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yk_user_header_img"]];
    [topImageView addSubview:avatarIcon];
    [avatarIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(topImageView.mas_leading).offset(12);
        make.centerY.equalTo(topImageView.mas_centerY);
        
        make.size.mas_equalTo(CGSizeMake(62, 62));
    }];
    
    UILabel* usernameLabel = [[UILabel alloc]init];
    usernameLabel.text = tab.userModel.USER_NAME;
    usernameLabel.textColor = [UIColor whiteColor];
    [avatarIcon addSubview:usernameLabel];
    
//    UILabel* groupLabel = [[UILabel alloc]init];
//    groupLabel.text = @"";
//    groupLabel.textColor = [UIColor whiteColor];
//    groupLabel.font = [UIFont systemFontOfSize:14];
//    [avatarIcon addSubview:groupLabel];
//
//    UILabel* partLabel = [[UILabel alloc]init];
//    partLabel.text = @"";
//    partLabel.textColor = [UIColor whiteColor];
//    partLabel.font = [UIFont systemFontOfSize:14];
//    [avatarIcon addSubview:partLabel];
    
    [usernameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(avatarIcon.mas_trailing).offset(12);
//        make.bottom.equalTo(avatarIcon.mas_centerY);
        make.top.equalTo(avatarIcon.mas_top);
    }];
    
//    [groupLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(usernameLabel.mas_leading);
//        make.top.equalTo(usernameLabel.mas_bottom).offset(2);;
//    }];
//    
//    [partLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(usernameLabel.mas_leading);
//        make.top.equalTo(groupLabel.mas_bottom).offset(2);
//    }];
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImageView.mas_bottom);
        make.leading.trailing.equalTo(self.view);
        make.height.mas_equalTo(44 * 5);
    }];
    self.tableView.bounces = NO;
    TFTableViewSection *section = [TFTableViewSection section];
    section.headerHeight = .1f;
    section.footerHeight = .1f;
    TFTextFieldWithTitleItem.titleWidth = 75;
    self.accountItem = [TFTextFieldWithTitleItem itemWithTitle:@"登录账号" placeholder:nil value:tab.userModel.USER_ID enabled:NO selectionHandler:nil clickHandler:nil];
    self.accountItem.cellHeight = 44;
    [section addItem:self.accountItem];
    
    TFTextFieldWithTitleItem *photoItem = [TFTextFieldWithTitleItem itemWithTitle:@"手机号码" placeholder:nil value:tab.userModel.LINK_PHONE enabled:NO selectionHandler:nil clickHandler:nil];
    photoItem.cellHeight = 44;
    self.photoItem = photoItem;
    [section addItem:photoItem];
    
    TFTextFieldWithTitleItem *emailItem = [TFTextFieldWithTitleItem itemWithTitle:@"电子邮箱" placeholder:nil value:tab.userModel.LINK_EMAIL enabled:NO selectionHandler:nil clickHandler:nil];
    
    emailItem.cellHeight = 44;
    self.emailItem = emailItem;
    [section addItem:emailItem];
    
    TFTitleDetailItem *passwordItem = [TFTitleDetailItem itemWithTitle:@"更改密码" detail:@""];
    passwordItem.cellHeight = 44;
    passwordItem.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [section addItem:passwordItem];
    passwordItem.selectionHandler = ^(__kindof TFTableViewItem *item, NSIndexPath *indexPath) {
        [YKPopupView showPopupViewWithViewController:[YKModifyPasswordViewController new] height:250];
    };
    
    TFTitleDetailItem *helpItem = [TFTitleDetailItem itemWithTitle:@"帮助文档" detail:@""];
    helpItem.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    helpItem.cellHeight = 44;
    [section addItem:helpItem];
    @weakify(self)
    helpItem.selectionHandler = ^(__kindof TFTableViewItem *item, NSIndexPath *indexPath) {
        YKHelpDocumentsViewController *vc = [[YKHelpDocumentsViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self_weak_.navigationController pushViewController:vc animated:YES];
    };
    [self.tableViewManager addSection:section];
    [RACObserve(tab, userModel) subscribeNext:^(id x) {
        
        if (x) {
            usernameLabel.text = tab.userModel.USER_NAME;
//            phoneLabel.text = tab.userModel.LINK_PHONE;
            
            self.accountItem.title = @"登录账号:";
            self.accountItem.value = tab.userModel.USER_ID;
            photoItem.title = @"手机号码:";
            emailItem.title = @"电子邮箱:";
            photoItem.value = tab.userModel.LINK_PHONE;
            emailItem.value =tab.userModel.LINK_EMAIL;
            [self_weak_.tableView reloadData];
            
            
//            [TFHTTPMANAER POST:@"personalCentr/searchBasicDataByUserId.htm" parameters:@{@"id": tab.userModel.ID} success:^(id responseObject) {
//                NSString * PART_NAME = [responseObject valueForKeyPath:@"userInfo.PART_NAME"];
//                NSString * ROLE_NAME = [responseObject valueForKeyPath:@"userInfo.ROLE_NAME"];
//
//            } failure:^(id responseObject, NSError *error) {
//
//            }];
            
        }
    }];
    
    UIBarButtonItem *editItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(onEditItemTapped:)];
    self.navigationItem.rightBarButtonItem = editItem;
    
    UIButton* confirmButton = [[UIButton alloc]init];
    [confirmButton setTitle:@"注销" forState:UIControlStateNormal];
    [confirmButton setBackgroundImage:TFSTYLE(navigationBarBackgroudImage) forState:UIControlStateNormal];
    
    [confirmButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor tf_colorWithHexString:@"#999999"]] forState:UIControlStateDisabled];
    
    [confirmButton setContentEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    confirmButton.tf_cornerRadius = 4.0f;
    
    [self.view addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(15);
        make.trailing.equalTo(self.view.mas_trailing).offset(-15);
        make.top.equalTo(self.tableView.mas_bottom).offset(30);
    }];
    
    confirmButton.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        [TFHTTPMANAER POST:@"login/exit.htm" parameters:nil HUD:nil success:^(id responseObject) {
            [YKTabBarViewController sharedInstance].userModel = nil;
            [[YKTabBarViewController sharedInstance]viewDidAppear:NO];
        } failure:^(id responseObject, NSError *error) {
            [YKTabBarViewController sharedInstance].userModel = nil;
            [[YKTabBarViewController sharedInstance]viewDidAppear:NO];
        }];
        return [RACSignal empty];
    }];
    
    UILabel *versionLabel = [[UILabel alloc]init];
    [self.view addSubview:versionLabel];
    [versionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-10);
    }];
    versionLabel.textAlignment = NSTextAlignmentCenter;
    versionLabel.numberOfLines = 0;
    versionLabel.text = [NSString stringWithFormat:@"联系我们：025-84578088\n系统版本V%@",[[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
    
}

- (void)onEditItemTapped:(UIBarButtonItem*)item {
    if ([item.title isEqualToString:@"编辑"]) {
        self.emailItem.enabled = YES;
        self.photoItem.enabled = YES;
        self.emailItem.borderStyle = UITextBorderStyleRoundedRect;
        self.photoItem.borderStyle = UITextBorderStyleRoundedRect;
        self.accountItem.inputBackgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
        [self.tableView reloadData];
        item.title = @"完成";
    }
    else {
        [TFHTTPMANAER POST:@"personalCentr/doEdtBasicDataById.htm" parameters:@{@"id":[YKTabBarViewController sharedInstance].userModel.ID, @"user_name":[YKTabBarViewController sharedInstance].userModel.USER_NAME,@"link_phone":self.photoItem.value, @"link_email":self.emailItem.value} HUD:nil success:^(id responseObject) {
            [self showHint:@"修改成功"];
            self.emailItem.enabled = NO;
            self.photoItem.enabled = NO;
            self.emailItem.borderStyle = UITextBorderStyleNone;
            self.photoItem.borderStyle = UITextBorderStyleNone;
            self.accountItem.inputBackgroundColor = [UIColor clearColor];
            item.title = @"编辑";
            [self.tableView reloadData];
        } failure:^(id responseObject, NSError *error) {
            [self showHint:error.localizedFailureReason];
        }];
    }
}

@end
